﻿Imports TwitterVB
Imports System.Threading
Imports System.ComponentModel
Class MainWindow
    Dim tw As New TwitterVB.Twitter
    Dim ConsumerKey = "ONY0fEUfkFkNO5rScphfQ"
    Dim ConsumerKeySecret = "VMfsrOYlh59C2ZjkGBg001X7ShZ3UbbL4CE7UQkbMU"
    Dim OAuthToken = "50248369-7NeluYsQEoB8FF5WCWYBuVeSgkky6dGxGbAJGIiiE" 'My.Settings.Twitter_OAuthToken
    Dim OAuthTokenSecret = "fpwOU689U92M8gHWOZiVW7jQjZtto3KwuCzlbnKDbg0" 'My.Settings.Twitter_OAuthTokenSecret
    Dim x As twits
    Dim son_twitter_id As Long
    Dim sayfa As Integer = 1
    Dim WithEvents workerThread1 As Thread
    Public bilgi As New bilgiler

    Public Function FindScroll(ByRef lstBox As ListBox) As ScrollViewer
        Dim objScroll As ScrollViewer
        Dim obj As Object
        Try
            obj = VisualTreeHelper.GetChild(lstBox, 0)
            Dim intI As Integer = 0
            While (obj IsNot Nothing AndAlso Not obj.GetType Is GetType(Border))
                obj = VisualTreeHelper.GetChild(lstBox, 0)
            End While
            Dim objBorder As Border
            objBorder = obj
            objScroll = objBorder.Child
        Catch ex As Exception
            Return Nothing
        End Try
        Return objScroll
    End Function

    Public Sub ilk_yukleme()
        bilgi.calisiyor = True

        Dim tp As New TwitterParameters
        tp.Add(TwitterParameterNames.Count, 20)
        tp.Add(TwitterParameterNames.Page, sayfa)
        sayfa += 1

        Dim _Timeline = tw.TimelineMethods.FriendsTimeLine(tp)

        SyncLock x
            For Each Status As TwitterStatus In _Timeline
                Dim y = New twit(Status.ID, Status.TwitterUser.ProfileImageUrl, Status.Text)
                x.Add(y)
            Next
        End SyncLock
        bilgi.calisiyor = False
    End Sub
    Public Sub yenileme()
        bilgi.calisiyor = True
        Dim tp As New TwitterParameters
        tp.Add(TwitterParameterNames.SinceID, x(0).id)
        Dim _Timeline = tw.TimelineMethods.FriendsTimeLine(tp)
        If IsNothing(_Timeline) = False Then
            SyncLock x
                For Each Status As TwitterStatus In _Timeline
                    Dim y = New twit(Status.ID, Status.TwitterUser.ProfileImageUrl, Status.Text)
                    x.Insert(0, y)
                Next
            End SyncLock
        End If
        bilgi.calisiyor = False
    End Sub
    Public Sub scroll()
        bilgi.calisiyor = True
        Dim var = New Boolean = False
        Dim tp As New TwitterParameters
        tp.Add(TwitterParameterNames.Count, 20)
        tp.Add(TwitterParameterNames.Page, sayfa)
        sayfa += 1
        Dim _Timeline = tw.TimelineMethods.FriendsTimeLine(tp)
        If IsNothing(_Timeline) = False Then
            SyncLock x
                For Each Status As TwitterStatus In _Timeline
                    Dim y = New twit(Status.ID, Status.TwitterUser.ProfileImageUrl, Status.Text)
                    var = False
                    For i = 0 To x.Count - 1
                        If x.Item(i).id = Status.ID Then
                            var = True
                        End If
                    Next
                    If var = False Then x.Add(y)
                Next
            End SyncLock
        End If
        bilgi.calisiyor = False
    End Sub

    Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        x = New twits(ListBox1.Dispatcher)
        Me.DataContext = bilgi
        ListBox1.DataContext = x


        FindScroll(ListBox1).AddHandler(ScrollViewer.ScrollChangedEvent, New RoutedEventHandler(AddressOf ScrollChanged))

        tw.AuthenticateAs("ssessizz", "53515351")
        'tw.AuthenticateWith(ConsumerKey, ConsumerKeySecret, OAuthToken, OAuthTokenSecret)


        workerThread1 = New Thread(New ThreadStart(AddressOf ilk_yukleme))
        workerThread1.Start()
    End Sub
    Public Sub ScrollChanged(ByVal sender As ScrollViewer, ByVal e As System.Windows.RoutedEventArgs)
        TextBox1.Text = sender.VerticalOffset & " " & sender.ScrollableHeight & " " & tw.RemainingHits
        If (sender.VerticalOffset = sender.ScrollableHeight And sender.ScrollableHeight <> 0 And bilgi.calisiyor = False) Or (sender.VerticalOffset = 0 And sender.ScrollableHeight = 0 And bilgi.calisiyor = False) Then
            workerThread1 = New Thread(New ThreadStart(AddressOf scroll))
            workerThread1.Start()
        End If
    End Sub
    Dim progress As Integer = 0
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        'workerThread1 = New Thread(New ThreadStart(AddressOf yenileme))
        'workerThread1.Start()
        TextBlock1.Inlines.Add("Click the ")
        TextBlock1.Inlines.Add(New Italic(New Run("Button")))
        TextBlock1.Inlines.Add(" to")
        'TextBlock1.Inlines.Add(New LineBreak())
        TextBlock1.Inlines.Add("launch the ")
        TextBlock1.Inlines.Add(New Bold(New Run("Rocket")))
    End Sub
End Class
